package android.bignerdranch.videoplayer;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    Button btn1,btn2;
    EditText editText;
    ListView listview;
    String path;
    SharedPreferences sharedPreferences;
    SharedPreferences.Editor editor;
    private static final String PREFS_NAME ="android.bignerdranch.videoplayer" ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn1 = findViewById(R.id.bt_1);
        btn2 = findViewById(R.id.bt_2);
        editText = findViewById(R.id.ed_1);
        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listview = new ListView(MainActivity.this);//搜索手机中的视频文件
                Cursor c = MainActivity.this.getContentResolver()
                        .query(MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
                                new String[]{MediaStore.Video.Media.TITLE,
                                        MediaStore.Video.Media.DURATION,
                                        MediaStore.Video.Media._ID,
                                        MediaStore.Video.Media.DISPLAY_NAME,
                                        MediaStore.Video.Media.DATA},
                                null, null, null);
                if (c == null || c.getCount() == 0)//如果没有搜索到视频，显示存储列表为空...
                {
                    AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                    builder.setMessage("存储列表为空...").setPositiveButton("确定", null);
                    AlertDialog ad = builder.create();
                    ad.show();
                } else{
                    sharedPreferences = getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
                    editor = sharedPreferences.edit();
                    if(editText.getText().toString().endsWith(".mp4"))
                    {

                        path = editText.getText().toString();
                        editor.putString("path",path);
                        editor.apply();
                    }
                    else {
                        SharedPreferences userInfo = getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
                        SharedPreferences.Editor editor = userInfo.edit();//获取Editor
                        editor.clear();
                        editor.putInt("isInput",0);
                        editor.apply();
                    }
                    Intent intent = new Intent();
                    intent.setClass(MainActivity.this, PlayerActivity.class);
                    startActivity(intent);
                }
                assert c != null;
                c.close();
            }
        });
        btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(MainActivity.this,LocalVideoListActivity.class);
                startActivity(intent);
            }
        });
    }
}
